package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.cache.redis.RedisKeyBuilder;
import com.gitee.apanlh.util.cache.redis.api.RedisString;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.log.LogLevel;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpResponse;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpMethod;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoDaLian;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 * 安心分期-龙岩市企诺API全量15 大连
 * TOKEN请求KEY目前有问题对方未提供
 * @author 李源聪
 */
@ApiPlan(productId = "304", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5LongYnaQiNuoDaLian implements ApiOrgPhoneMd5ConvertHandler {
    private static final String ACCESS_ID = "1751853351905";
    private static final String ACCESS_Key = "a12cc95cda3e41348d0ec8511ad2b359";

    private static final String RESULT_URL = "http://dxapi.51xxzhbei.cn/openapi/GetAccessToken.aspx";

    private static final RedisString TEMP = RedisKeyBuilder.builder().withKeys("com.ts.api.http.receive.biz.ApiOrgPhoneMd5LongYnaQiNuoDaLian").buildString();

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<String, Object> sendMap = MapUtils.newLinkedHashMap();
        sendMap.put("accessID", ACCESS_ID);
        sendMap.put("accessKey", ACCESS_Key);
        HttpResponse build = HttpClientBuilder.builder(RESULT_URL, HttpMethod.POST)
                .withBodyJson(sendMap)
                .withHeader((headerMap) -> headerMap.put("Content-Type", "application/json"))
                .withLogLevel(LogLevel.WARN)
                .withErrorLogLevel(LogLevel.WARN)
                .build();
        String respStr = build.getStr();
        JSONObject jsonObject = JsonUtils.get(respStr);
        String code = jsonObject.getString("orsuccess");
        if (!Eq.object(code, "1")) {
            throw new RuntimeException("获取TOKEN失败" + respStr);
        }
        String token = jsonObject.getString("token");
        TEMP.setForSeconds(apiDataExchangeParameter.getReqId(), token, 300);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("accessID", ACCESS_ID);
            newMap.put("Phone", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("token", token);
        }));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("orsuccess"), 1);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        OrgApplyLongYanQiNuoDaLian orgApplyLongYanQiNuoDaLian = new OrgApplyLongYanQiNuoDaLian();
        orgApplyLongYanQiNuoDaLian.setF1(apiApplyParameter.getUserName());
        orgApplyLongYanQiNuoDaLian.setF2(apiApplyParameter.getPhone());
        //  职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", "1")
                    .when(apiApplyParameter.getCareer(), "2", "1")
                    .when(apiApplyParameter.getCareer(), "3", "2")
                    .when(apiApplyParameter.getCareer(), "4", "1")
                    .when(apiApplyParameter.getCareer(), "5", "1")
                    .end("1");
            orgApplyLongYanQiNuoDaLian.setF3(text);
        }
        orgApplyLongYanQiNuoDaLian.setF4(String.valueOf(apiFieldConverter.getLoanAmountInt()));
        orgApplyLongYanQiNuoDaLian.setF6(apiFieldConverter.getExtendInfo());
        orgApplyLongYanQiNuoDaLian.setAccessID(ACCESS_ID);
        String reqNoInfo = TEMP.get(apiApplyParameter.getReqId(), String.class);
        orgApplyLongYanQiNuoDaLian.setToken(reqNoInfo);
        return JsonUtils.toJson(orgApplyLongYanQiNuoDaLian);

    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("orsuccess"), 1)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
